package com.yisoo.ubag.iot.aliyun.sdk.openapi;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.*;
import com.yisoo.ubag.iot.aliyun.util.LogUtil;
import org.apache.commons.codec.binary.Base64;

import java.util.List;


/**
 * mmciel
 * 2021年2月10日22:32:25
 */
public class PubManager {

    private static DefaultAcsClient client = AbstractManager.getClient();

    /**
     * mmciel 发送topic
     * @param content 内容
     * @param productKey 产品
     * @param topic 主题
     * @param qos 0或者1 1可以保存
     * @return
     */
    public static PubResponse PubRequest(String content,String productKey,
                                         String topic,Integer qos) {
//        //TODO 因为硬件坏了，所以这里的MQTT调试关掉。
//        System.out.println("@@@@@成功发送："+content);
//        return null;

        PubRequest request = new PubRequest();
        PubResponse response = null;
        request.setProductKey(productKey);
        request.setMessageContent(Base64.encodeBase64String(content.getBytes()));
        request.setTopicFullName(topic);
        request.setQos(qos); //目前支持QoS0和QoS1。
        try
        {
            System.out.println("客户端："+client);
            response = client.getAcsResponse(request);
            if (response.getSuccess() != null && response.getSuccess()) {
                LogUtil.print("发送topic成功");
                LogUtil.print(JSON.toJSONString(response));
            } else {
                LogUtil.print("发送topic失败");
                LogUtil.error(JSON.toJSONString(response));
            }
            return response;
        } catch (ClientException e)
        {
            e.printStackTrace();
            LogUtil.error("发送topic失败！");
        }
        return null;


    }
}
